import fs = require("fs")
import path = require("path")
import { ServerConfig } from "../../../config/ServerConfig";
import { HttpUtil } from "../../utils/HttpUtil";
import { Logger } from "../../utils/Logger";
import { SocketConnection } from "./SocketConnection";

export class SocketConnectionManager {

    public static instance: SocketConnectionManager = new SocketConnectionManager();

    public socketConnMap: Map<number, SocketConnection> = new Map<number, SocketConnection>();

    /* 
   增加客户端socket连接
    */
    public addSocketConnection(conn: SocketConnection) {
        this.socketConnMap.set(conn.id, conn);
    }

    public getSocketConnection(id: number) {
        return this.socketConnMap.get(id);
    }

    public getSocketConnectionByUser(account: string): Array<SocketConnection> {
        let result: Array<SocketConnection> = []
        this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (!conn.isClosing && conn.account && conn.account == account) {
                result.push(conn)
                return;
            }
        });
        return result
    }

    /* 
    删除客户端socket连接
     */
    public removeSocketConnection(removeOne: SocketConnection) {
        if (!removeOne) {
            return;
        }
        this.socketConnMap.delete(removeOne.id);
    }

    public getConnectionCount() {
        let count: number = 0
        this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                // if(conn.user){
                //     count++
                // }
                // Logger.log("getConnectionCount==", conn.account, conn.user)
            }
        });
        return count;
    }

    public getClubOnlineCount(clubid: number) {
        let count: number = 0;
        SocketConnectionManager.instance.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                // if (conn.user) {
                //     for (let i = 0; i < conn.user.clubList.length; i++) {
                //         let club: ClubModel = conn.user.clubList[i];
                //         if (club.clubid == clubid) {
                //             count++;
                //             break;
                //         }
                //     }
                // }
            }
        });
        return count
    }

    public isUserOnline(userid: number) {
        let isOnline: boolean = false
        SocketConnectionManager.instance.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                // if (conn.user && conn.user.userid == userid) {
                //     isOnline = true
                //     return;
                // }
            }
        });
        return isOnline
    }

    public getSocketConnectionByUserid(userid: number): SocketConnection {
        let connection:SocketConnection = null
        this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                // if (conn.user && conn.user.userid == userid) {
                //     connection = conn
                //     return conn;
                // }
            }
        });
        return connection
    }

}